package com.etc.controller;


import com.etc.Student;
import com.etc.common.CodeMessage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

/**
 * 所有的响应都是 json形式 @ResponseBody
 * @RespnoseBody + @Controller  == @RestController
 */
@RestController
public class HelloController {

    // http://localhost:8888/hello?ename=tom&age=30&aaa=bbbh
    // 请求是hello，get的时候 运行这个方法 -- handleMapping
    // 参数列表@RquestParam 映射到HashMap上、
    // 返回值HashMap  --生成json数据 响应个客户端
    @GetMapping("hello")
    public HashMap hello(@RequestParam HashMap params) {

        params.forEach((k, v) -> System.out.println(k + "@" + v));

        HashMap result = new HashMap();
        result.put("code", 20000);
        result.put("message", "成功");
        result.put("data", new String[]{"aaa", "bbbb"});
        return result;
    }

    // http://localhost:8888/hello
    // ename=tom&age=30&aaa=bbbh
    // 同名就可以
    @PostMapping("hello")
    public CodeMessage hello(String ename, @RequestParam(value="age") int age1, String aaa) {

        System.out.println(ename);
        System.out.println(age1);
        System.out.println(aaa);

        return CodeMessage.success(new Date());
    }

//    // http://localhost:8888/hello
//    // ename=tom&age=30&aaa=bbbh
//    // 同名就可以
//    @PostMapping("hello")
//    public CodeMessage hello(@RequestBody Student stu) {
//
////        System.out.println(ename);
//        System.out.println(stu);
////        System.out.println(aaa);
//
//        return CodeMessage.success(new Date());
//    }

    // http://localhost:8888/hello?ename=tom&age=2313&aaa=bbbadfs
    @PutMapping("hello")
    public String hello(Student stu, HttpServletRequest request) {

        String xtoken = request.getHeader("x-token");
        System.out.println(xtoken);

        System.out.println(stu);


        return "jsdjaflaksdjfasldk;fjal;skdfja;lksfj";
    }





}
